class Request {
  //https:///m.zhiliaoyuedu.cn
  //baseUrl = "https:///dujun1122.natapp4.cc/app/consumer";
  //baseUrl = "https://m.zhiliaoyuedu.cn/app/consumer"
  baseUrl = "http://localhost:8080"
  getData({
    url,
    method = 'GET',
    data = {}
  }) {

    return new Promise((resolve, reject) => {
      var token = wx.getStorageSync('token')
      console.log("请求数据:", data)
      if(token)
      token = 'Bearer ' + token;
      wx.request({
        url: this.baseUrl + url,
        method: method,
        header: {
          'Authorization': token,
          'content-type': method == 'POST' ? 'application/json' : 'application/x-www-form-urlencoded'
        },
        data: method == 'POST' ? JSON.stringify(data) : data,
        success: res => {
          console
          console.log("返回数据:", res.data)
          if (res.data.code == 200) {
            resolve(res.data)
          } else {
            this.showError(res.data.msg)
          }
        },
        fail: err => {
          console.log("err:", err)
          reject(err)
          this.showError()
        }
      })
    })
  }

  getDictData({
    method = 'GET',
    data = {}
  }) {
    return new Promise((resolve, reject) => {
      var token = wx.getStorageSync('token')
      wx.request({
        url: 'https://m.zhiliaoyuedu.cn/sys/dict/all',
        //url: 'http://localhost:80/sys/dict/all',
        method: method,
        header: {
          'Authorization': token,
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: data,
        success: res => {
          console.log("返回数据:", res.data)
          if (res.data.code == 0) {
            resolve(res.data)
          } else {
            this.showError(res.data.msg)
          }
        },
        fail: err => {
          console.log("err:", err)
          reject(err)
          this.showError()
        }
      })
    })
  }

  showError(error) {
    if (error) {
      wx.showToast({
        title: error,
        icon: "none"
      })
    } else {
    }
  }
}

export {
  Request
}